var app = getApp()
Page({
  data: {
    navLeftItems: [],
    navRightItems: [],
    curNav: 1,
    curIndex: 0,
    ids:"",
    progressId:""
  },
  onLoad: function (option) {
    this.setData({
      progressId: option.id,
    })
    var userInfo = wx.getStorageSync('userInfo');
    var that = this
    wx.request({
      url: app.globalData.BaseURL +'/product',
      method: 'GET',
      data: {},
      header: {
        'Accept': 'application/json',
        'Authorization': userInfo.token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            that.setData({
              navLeftItems: res.data.data,
              navRightItems: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        } else {
          wx.navigateTo({
            url: '../auth/auth',
          })
        }
      }
    })
  },
  onPostClick: function(e){
    var progressId = this.data.progressId;
    var userInfo = wx.getStorageSync('userInfo');
    var ids = "";
    this.data.navRightItems.forEach(function (item) {
      if (item.children!=null){
        item.children.forEach(function (child) {
          if (child.pick == true) {
            ids += child.id + ",";
          }
        })
      }
    });
    ids = ids.substring(0, ids.lastIndexOf(','));  
    wx.request({
      url: app.globalData.BaseURL + '/customerProduct',
      method: 'POST',
      data: {
        progressId: progressId,
        ids:ids
      },
      header: {
        'Authorization': userInfo.token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '../index/index',
            })
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        } else {
          wx.reLaunch({
            url: '../auth/auth',
          })
        }
      }
    })
  },

  //事件处理函数
  switchRightTab: function (e) {
    let id = e.target.dataset.id,
      index = parseInt(e.target.dataset.index);
    this.setData({
      curNav: id,
      curIndex: index
    })
  },
  pick: function (e){
    var userInfo = wx.getStorageSync('userInfo');
    var id =e.currentTarget.dataset.id;
    var that =this;
    let tmp = this.data.navRightItems;
    if (tmp[this.data.curIndex].children[e.currentTarget.dataset.index].pick == true){
      tmp[this.data.curIndex].children[e.currentTarget.dataset.index].pick = false;
    }else{
      tmp[this.data.curIndex].children[e.currentTarget.dataset.index].pick = true;
      
    }
    that.setData({
      navRightItems: tmp
    })
    
  }
})